/*============================================================
MONTHLY COSTING CALCULATION
create by : Duong Quoc An
create date: 19/09/2013
=============================================================*/
{mfdtitle.i "g"}
/*================================DEF MESSAGE=========================================*/
DEF VAR messageWarning  AS CHARACTER FORMAT "x(100)".
DEF VAR messageW        AS CHARACTER EXTENT 5
    INIT ["This program used to calculate monthly costing.","For correct result, you must be already posted all of operational transactions","     (run Operational Transaction Post), and recorded all of financial documents.",
"In first, you can check result which running program is without updated check box.","Then, you can checked box again in order to final costing method."].
/*================================DEF VAR=============================================*/
DEF VAR imonth          AS INTEGER FORMAT "99".
DEF VAR iyear           AS INTEGER FORMAT "9999".
DEF VAR ilastmonth      AS INTEGER FORMAT "99".
DEF VAR ilastyear       AS INTEGER FORMAT "9999".
DEF VAR inextmonth      AS INTEGER FORMAT "99".
DEF VAR inextyear       AS INTEGER FORMAT "9999".
DEF VAR iupdate         AS LOGICAL INIT FALSE.
DEF VAR icheck          AS LOGICAL INIT FALSE.
DEF VAR imasterial      LIKE tr_hist.tr_part FORMAT "x(15)" LABEL "Material".
DEF VAR ifinishGood     LIKE pt_mstr.pt_part FORMAT "x(15)" LABEL "Finish Good".
DEF VAR isite           LIKE tr_hist.tr_site LABEL "Site".
DEF VAR iqtyMasterial   AS DECIMAL FORMAT "->,>>>,>>>,>>9.<<<<<<<<<" LABEL "Qty Material".
DEF VAR iavgCost        AS DECIMAL FORMAT "->,>>>,>>>,>>>.99" LABEL "Average Cost".
DEF VAR iqtyFG          AS DECIMAL FORMAT "->,>>>,>>>,>>>.99" LABEL "Qty FGood".
DEF VAR iavgRawMasCost  AS DECIMAL FORMAT "->,>>>,>>>,>>>.99" LABEL "ARM Cost".
DEF VAR iactualCost     AS DECIMAL FORMAT "->,>>>,>>>,>>>.99" LABEL "Actual Cost".
DEF VAR ireceiptCost    AS DECIMAL FORMAT "->,>>>,>>>,>>>.99" LABEL "WO Receipt Cost".
DEF VAR iworeceiptCost  AS DECIMAL FORMAT "->,>>>,>>>,>>>.99" LABEL "Receipt Cost".
DEF VAR icosting        AS DECIMAL FORMAT "->,>>>,>>>,>>9" LABEL "Costing".
DEF VAR i               AS INTEGER.
DEF VAR updateposting   AS DECIMAL.

DEF VAR checkUpdate     AS LOGICAL INIT FALSE.

DEF VAR iupdategl       LIKE GL.GL_ID.
DEF VAR sumrctwo        AS DECIMAL.
DEF VAR sumqtyrctwo     AS DECIMAL.
DEF VAR getidwo         AS CHARACTER.
DEF VAR getindex        AS INTEGER.
DEF VAR sumarmcost      AS DECIMAL.
DEF VAR sumqtybyloc     AS DECIMAL.
DEF VAR sumqtybyitem    AS DECIMAL.
DEF VAR icount          AS INTEGER.
DEF VAR flag            AS LOGICAL.
DEF VAR sumlastmonth    AS DECIMAL.

/*================================DEF TEMP TABLE======================================*/  
DEF TEMP-TABLE temptable
    FIELD masterial         LIKE tr_hist.tr_part
    FIELD finishGood        LIKE pt_mstr.pt_part
    FIELD site              LIKE tr_hist.tr_site
    FIELD qtyMasterial      AS DECIMAL
    FIELD avgCost           AS DECIMAL
    FIELD qtyFG             AS DECIMAL
    FIELD avgRawMasCost     AS DECIMAL
    FIELD actualCost        AS DECIMAL
    FIELD woreceiptCost     AS DECIMAL
    FIELD receiptCost       AS DECIMAL
    FIELD costing           AS DECIMAL
    FIELD sumavgRawMasCost  AS DECIMAL
    .

DEF TEMP-TABLE tempfg
    FIELD tempfg_month           AS INTEGER
    FIELD tempfg_year           AS INTEGER
    FIELD tempfg_site           LIKE si_mstr.si_site
    FIELD tempfg_part           LIKE pt_mstr.pt_part
    FIELD tempfg_sumarm           AS DECIMAL
    .
    
DEFINE TEMP-TABLE tCostMonthly LIKE CostMonthly.

/*================================DEF BUFFER===========================================*/

DEFINE BUFFER buf_CostMonthly FOR CostMonthly.
DEFINE BUFFER buf_tr_hist FOR tr_hist.
DEFINE BUFFER buf1_tr_hist FOR tr_hist.
DEFINE BUFFER buf_wo FOR wo_mstr.

/*================================DEF FRAME===========================================*/

FORM
    messageWarning NO-LABELS
    WITH TITLE "Warning" FRAME w DOWN ROW 10 WIDTH 200.

FORM 
    imonth  COLON 15 LABEL "Month :" iyear COLON 25 LABEL "Year :" SKIP(.5)
    iupdate COLON 15 LABEL "Update :" SKIP(.5)
    icheck  COLON 15 LABEL "Finalized :"
    WITH TITLE "Month/Year" FRAME s WIDTH 200 SIDE-LABELS.
    
/*================================Show Messsage======================================*/  
REPEAT i = 1 TO 5:
    messageWarning = messageW[i].
    DISP messageWarning WITH FRAME w.
    DOWN WITH FRAME w.
END.
/*================================Initial Value======================================*/

EMPTY TEMP-TABLE temptable.
EMPTY TEMP-TABLE tCostMonthly.
EMPTY TEMP-TABLE tempfg.
    
DEF VAR global_company LIKE Company.Company_ID.
FIND FIRST Domains WHERE Domains.DomainCode = global_domain NO-LOCK NO-ERROR.
IF (AVAILABLE Domains) THEN DO:
  FIND FIRST Company WHERE Domains.Domain_ID = Company.Domain_ID AND Company.CompanyCode = current_entity NO-LOCK NO-ERROR.
  IF AVAILABLE Company THEN DO:
      global_company = Company.Company_ID.
  END.
END.

imonth  = MONTH(TODAY).
iyear   = YEAR(TODAY).

mainloop:
REPEAT:
    iupdate = FALSE.
    icheck = FALSE.
    EMPTY TEMP-TABLE temptable.
    messageWarning = "".
    DISP messageWarning WITH FRAME w.
    
    UPDATE imonth iyear WITH FRAME s.  
    ilastmonth = imonth - 1.
    ilastyear = iyear.
    IF (ilastmonth = 0) THEN DO:
        ilastmonth = 12.
        ilastyear = ilastyear - 1.
    END.
    
    inextmonth = imonth + 1.
    inextyear = iyear.
    IF (inextmonth = 13) THEN DO:
        inextmonth = 1.
        inextyear = inextyear + 1.
    END.
    
    /*Check costing is updated or not*/
    FIND FIRST CostMonthly WHERE CostMonthly.CostMonthly_year = iyear AND CostMonthly.CostMonthly_month = imonth NO-LOCK NO-ERROR.
    IF (AVAILABLE CostMonthly AND CostMonthly.CostMonthly_finalized = FALSE) OR (NOT AVAILABLE CostMonthly) THEN DO:
        checkUpdate = TRUE.
        MESSAGE "This monthly costing is not updated or is not finalized.".
    END.
    ELSE DO:
        checkUpdate = FALSE.
        MESSAGE "This monthly costing finalized.".
        UNDO mainloop, RETRY mainloop.
    END.
    
    /*Update 2 checkboxes*/
    IF checkUpdate THEN DO:
        UPDATE iupdate WITH FRAME s.
        IF iupdate THEN DO:
          UPDATE icheck WITH FRAME s.
        END.
        ELSE DO:
          DISPLAY icheck WITH FRAME s.
        END.
    END.
    ELSE DO:
        iupdate = TRUE.
        icheck = TRUE.
        DISP iupdate icheck WITH FRAME s.   
    END.
    
    /*----------------------------------------------------------------*/
    /* ISS-SO: xuat cho ban hang san xuat */
    
    iqtyMasterial = 0.
    
    FOR EACH buf_tr_hist NO-LOCK 
      WHERE buf_tr_hist.tr_type = "ISS-WO" 
        AND buf_tr_hist.tr_effdate >= DATE(imonth, 1, iyear) 
          AND buf_tr_hist.tr_effdate < DATE(inextmonth, 1, inextyear)
            AND buf_tr_hist.tr_domain = global_domain, 
              EACH wo_mstr NO-LOCK 
                WHERE buf_tr_hist.tr_domain = wo_mstr.wo_domain 
                  AND buf_tr_hist.tr_nbr = wo_mstr.wo_nbr 
                    AND buf_tr_hist.tr_lot = wo_mstr.wo_lot
    BREAK BY wo_mstr.wo_part BY buf_tr_hist.tr_site BY buf_tr_hist.tr_part:
        IF NOT (imonth = 12 AND iyear = 2013 AND buf_tr_hist.tr_part = "100219901") THEN DO:
            iqtyMasterial = iqtyMasterial + buf_tr_hist.tr_qty_loc.
            IF LAST-OF (buf_tr_hist.tr_part) THEN DO:
                CREATE temptable.
                ASSIGN 
                temptable.finishGood = wo_mstr.wo_part
                temptable.masterial = buf_tr_hist.tr_part
                temptable.site = buf_tr_hist.tr_site
                temptable.qtyMasterial = - iqtyMasterial
                temptable.qtyFG = 0
                .
                FIND FIRST mcf WHERE mcf.mcf_month = imonth AND mcf.mcf_year = iyear AND wo_mstr.wo_part = mcf.mcf_part AND buf_tr_hist.tr_site = mcf.mcf_site AND mcf.mcf_domain = global_domain NO-LOCK NO-ERROR.
                IF (AVAILABLE mcf) THEN DO:
                    temptable.qtyFG = mcf.mcf_qty.
                    temptable.actualCost = mcf.mcf_actcost.
                END.
                FIND FIRST mcf WHERE mcf.mcf_month = imonth AND mcf.mcf_year = iyear AND mcf.mcf_part = buf_tr_hist.tr_part AND mcf.mcf_site = buf_tr_hist.tr_site AND mcf.mcf_domain = global_domain NO-LOCK NO-ERROR.
                IF (NOT AVAILABLE mcf) THEN DO:
                    FIND FIRST mcm WHERE mcm.mcm_month = imonth AND mcm.mcm_year = iyear AND buf_tr_hist.tr_part = mcm.mcm_part AND buf_tr_hist.tr_site = mcm.mcm_site AND mcm.mcm_domain = global_domain NO-LOCK NO-ERROR.
                    IF (AVAILABLE mcm) THEN DO:
                        temptable.avgCost = mcm.mcm_unitcost.
                    END.
                END.
                ELSE DO:
                    IF (temptable.masterial = temptable.finishGood) THEN DO:
                        FIND FIRST buf_CostMonthly WHERE buf_CostMonthly.CostMonthly_month = ilastmonth AND buf_CostMonthly.CostMonthly_year = ilastyear AND buf_CostMonthly.CostMonthly_site = temptable.site 
                            AND buf_CostMonthly.CostMonthly_part = temptable.masterial AND buf_CostMonthly.CostMonthly_domain = global_domain NO-LOCK NO-ERROR.
                        IF AVAILABLE buf_CostMonthly THEN DO:
                            temptable.avgCost = buf_CostMonthly.CostMonthly_cost.
                        END.
                        ELSE DO:
                            FOR FIRST buf1_tr_hist  WHERE buf1_tr_hist.tr_type = "RCT-WO" AND MONTH(buf1_tr_hist.tr_effdate) = imonth AND YEAR(buf1_tr_hist.tr_effdate) = iyear 
                                AND buf1_tr_hist.tr_site = temptable.site AND buf1_tr_hist.tr_part = temptable.masterial NO-LOCK :
                                FOR EACH tr_hist NO-LOCK WHERE tr_hist.tr_lot = buf1_tr_hist.tr_lot AND tr_hist.tr_domain = buf1_tr_hist.tr_domain AND tr_hist.tr_type = "ISS-WO":
                                    FIND FIRST mcm WHERE mcm.mcm_month = imonth AND mcm.mcm_year = iyear AND tr_hist.tr_part = mcm.mcm_part AND tr_hist.tr_site = mcm.mcm_site AND mcm.mcm_domain = global_domain NO-LOCK NO-ERROR.
                                    IF (AVAILABLE mcm) THEN DO:
                                        temptable.avgCost = temptable.avgCost + mcm.mcm_unitcost * tr_hist.tr_qty_loc.
                                    END.
                                END.
                                sumqtybyloc = 0.
                                FOR EACH tr_hist NO-LOCK WHERE tr_hist.tr_lot = buf1_tr_hist.tr_lot AND tr_hist.tr_domain = buf1_tr_hist.tr_domain AND tr_hist.tr_type = "RCT-WO":
                                    sumqtybyloc = sumqtybyloc + tr_hist.tr_qty_loc.
                                END.  
                                temptable.avgCost = ABSOLUTE(temptable.avgCost / sumqtybyloc).
                            END.
                        END.
                    END.
                    ELSE DO:
                        temptable.avgCost = ?.
                    END.
                END.
                iqtyMasterial = 0.
                temptable.avgRawMasCost = temptable.qtyMasterial * temptable.avgCost / temptable.qtyFG.
                sumarmcost = sumarmcost + (temptable.qtyMasterial * temptable.avgCost / temptable.qtyFG).
            END.
            IF LAST-OF(wo_mstr.wo_part) THEN DO:
                CREATE tempfg.
                ASSIGN         
                tempfg_site = wo_mstr.wo_site         
                tempfg_part = wo_mstr.wo_part        
                tempfg_sumarm = sumarmcost
                sumarmcost = 0
                .
            END.
        END.
    END.
    FOR EACH temptable WHERE temptable.avgCost <> ?:
        FIND FIRST tempfg WHERE tempfg_site = temptable.site AND tempfg.tempfg_part = temptable.finishGood NO-ERROR.
        IF (AVAILABLE tempfg) THEN DO:
            temptable.woreceiptCost = tempfg_sumarm + temptable.actualCost.
            FIND FIRST mcf WHERE mcf.mcf_month = imonth AND mcf.mcf_year = iyear AND mcf.mcf_part = temptable.finishGood AND mcf.mcf_site = temptable.site AND mcf.mcf_domain = global_domain NO-LOCK NO-ERROR.
            IF (AVAILABLE mcf) THEN DO:
                temptable.receiptCost = ((tempfg_sumarm + temptable.actualCost) * temptable.qtyFG + (mcf_impttax + mcf_po + mcf_variance + mcf_trans)) / (temptable.qtyFG + mcf_qtyrc).
                FIND FIRST CostMonthly WHERE CostMonthly.CostMonthly_part = temptable.finishGood AND CostMonthly.CostMonthly_site = temptable.site 
                      AND CostMonthly.CostMonthly_year = ilastyear AND CostMonthly.CostMonthly_month = ilastmonth NO-LOCK NO-ERROR.
                IF AVAILABLE CostMonthly THEN DO:
                    temptable.costing = ((temptable.receiptCost * (temptable.qtyFG + mcf_qtyrc)) + (CostMonthly.CostMonthly_qty * CostMonthly.CostMonthly_cost)) / (temptable.qtyFG + CostMonthly.CostMonthly_qty + mcf_qtyrc).
                END.
                ELSE DO:
                    temptable.costing = temptable.receiptCost.
                END.
                FIND FIRST mcm WHERE mcm.mcm_month = imonth AND mcm.mcm_year = iyear AND mcm.mcm_part = temptable.finishGood AND mcm.mcm_site = temptable.site AND mcm.mcm_domain = global_domain NO-ERROR.
                IF (AVAILABLE mcm) THEN DO:
                    mcm.mcm_unitcost = temptable.costing.
                END.
            END.  
        END.
    END.
    REPEAT:
        FIND FIRST temptable WHERE temptable.avgCost = ? NO-LOCK NO-ERROR.
        IF (AVAILABLE temptable) THEN DO:
            FOR EACH temptable WHERE temptable.avgCost = ?:
                IF (temptable.masterial = temptable.finishGood) THEN DO:
                    FIND FIRST buf_CostMonthly WHERE buf_CostMonthly.CostMonthly_month = ilastmonth AND buf_CostMonthly.CostMonthly_year = ilastyear AND buf_CostMonthly.CostMonthly_site = temptable.site 
                        AND buf_CostMonthly.CostMonthly_part = temptable.masterial AND buf_CostMonthly.CostMonthly_domain = global_domain NO-LOCK NO-ERROR.
                    IF AVAILABLE buf_CostMonthly THEN DO:
                        temptable.avgCost = buf_CostMonthly.CostMonthly_cost.
                    END.
                    ELSE DO:
                        FOR FIRST buf1_tr_hist WHERE buf1_tr_hist.tr_type = "RCT-WO" AND MONTH(buf1_tr_hist.tr_effdate) = imonth AND YEAR(buf1_tr_hist.tr_effdate) = iyear 
                            AND buf1_tr_hist.tr_site = temptable.site AND buf1_tr_hist.tr_part = temptable.masterial NO-LOCK :
                            FOR EACH tr_hist NO-LOCK WHERE tr_hist.tr_lot = buf1_tr_hist.tr_lot AND tr_hist.tr_domain = buf1_tr_hist.tr_domain AND tr_hist.tr_type = "ISS-WO":
                                FIND FIRST mcm WHERE mcm.mcm_month = imonth AND mcm.mcm_year = iyear AND tr_hist.tr_part = mcm.mcm_part AND tr_hist.tr_site = mcm.mcm_site AND mcm.mcm_domain = global_domain NO-LOCK NO-ERROR.
                                IF (AVAILABLE mcm) THEN DO:
                                    temptable.avgCost = temptable.avgCost + mcm.mcm_unitcost * tr_hist.tr_qty_loc.
                                END.
                            END.
                            sumqtybyloc = 0.
                            FOR EACH tr_hist NO-LOCK WHERE tr_hist.tr_lot = buf1_tr_hist.tr_lot AND tr_hist.tr_domain = buf1_tr_hist.tr_domain AND tr_hist.tr_type = "RCT-WO":
                                sumqtybyloc = sumqtybyloc + tr_hist.tr_qty_loc.
                            END.  
                            temptable.avgCost = ABSOLUTE(temptable.avgCost / sumqtybyloc).
                        END.
                    END.
                END.
                ELSE DO:
                    FIND FIRST mcm WHERE mcm.mcm_month = imonth AND mcm.mcm_year = iyear AND mcm.mcm_part = temptable.masterial AND mcm.mcm_site = temptable.site AND mcm.mcm_domain = global_domain NO-ERROR.
                    IF (AVAILABLE mcm) THEN DO:
                        temptable.avgCost = mcm.mcm_unitcost.
                        temptable.avgRawMasCost = temptable.qtyMasterial * temptable.avgCost / temptable.qtyFG.
                    END.
                END. 
            END.
            EMPTY TEMP-TABLE tempfg.
            sumarmcost = 0.
            FOR EACH temptable BREAK BY temptable.finishGood:
                sumarmcost = sumarmcost + temptable.avgRawMasCost.
                IF LAST-OF(temptable.finishGood) THEN DO:
                    CREATE tempfg.
                    ASSIGN         
                    tempfg_site = temptable.site         
                    tempfg_part = temptable.finishGood        
                    tempfg_sumarm = sumarmcost
                    sumarmcost = 0
                    .
                END.
            END.
            FOR EACH temptable:
                FIND FIRST tempfg WHERE tempfg_site = temptable.site AND tempfg.tempfg_part = temptable.finishGood NO-ERROR.
                IF (AVAILABLE tempfg) THEN DO:
                    temptable.woreceiptCost = tempfg_sumarm + temptable.actualCost .
                    FIND FIRST mcf WHERE mcf.mcf_month = imonth AND mcf.mcf_year = iyear AND mcf.mcf_part = temptable.finishGood AND mcf.mcf_site = temptable.site AND mcf.mcf_domain = global_domain NO-LOCK NO-ERROR.
                    IF (AVAILABLE mcf) THEN DO:
                        temptable.receiptCost = ((tempfg_sumarm + temptable.actualCost) * temptable.qtyFG + (mcf.mcf_impttax + mcf.mcf_po + mcf.mcf_variance + mcf.mcf_trans)) / (temptable.qtyFG + mcf.mcf_qtyrc).
                        FIND FIRST CostMonthly WHERE CostMonthly.CostMonthly_part = temptable.finishGood AND CostMonthly.CostMonthly_site = temptable.site 
                              AND CostMonthly.CostMonthly_year = ilastyear AND CostMonthly.CostMonthly_month = ilastmonth NO-LOCK NO-ERROR.
                        IF AVAILABLE CostMonthly THEN DO:
                            temptable.costing = ((temptable.receiptCost * (temptable.qtyFG + mcf_qtyrc)) + (CostMonthly.CostMonthly_qty * CostMonthly.CostMonthly_cost)) / (temptable.qtyFG + CostMonthly.CostMonthly_qty + mcf_qtyrc).
                        END.
                        ELSE DO:
                            temptable.costing = temptable.receiptCost.
                        END.
                        FIND FIRST mcm WHERE mcm.mcm_month = imonth AND mcm.mcm_year = iyear AND mcm.mcm_part = temptable.finishGood AND mcm.mcm_site = temptable.site AND mcm.mcm_domain = global_domain NO-ERROR.
                        IF (AVAILABLE mcm) THEN DO:
                            mcm.mcm_unitcost = temptable.costing.
                        END.
                    END.  
                END.
            END.
            icount = 0.
            FOR EACH temptable WHERE temptable.avgCost = ?:
                FIND FIRST mcm WHERE mcm.mcm_month = imonth AND mcm.mcm_year = iyear AND mcm.mcm_part = temptable.masterial AND mcm.mcm_site = temptable.site AND mcm.mcm_domain = global_domain AND mcm.mcm_unitcost <> ? NO-ERROR.
                IF (AVAILABLE mcm) THEN DO:
                    icount = icount + 1.
                END.
            END.
            IF (icount = 0) THEN DO:
                LEAVE.
            END.
        END.
        ELSE DO:
            LEAVE.
        END.
    END.
    {mfselbpr.i "PRINTER" 132}
    {mfphead.i}
    
    FOR EACH temptable NO-LOCK BREAK BY temptable.finishGood BY temptable.masterial WITH FRAME b DOWN NO-ATTR-SPACE WIDTH 200:
        setFrameLabels(frame b:handle).
        
        imasterial = temptable.masterial.
        ifinishGood = temptable.finishGood.
        isite = temptable.site.
        iqtyMasterial = temptable.qtyMasterial.
        iavgCost = temptable.avgCost.
        iqtyFG = temptable.qtyFG.
        iavgRawMasCost = temptable.avgRawMasCost.
        iactualCost = temptable.actualCost.
        iworeceiptCost = temptable.woreceiptCost.
        ireceiptCost = temptable.receiptCost.
        icosting = temptable.costing.
        
        DISP ifinishGood imasterial isite iqtyMasterial iavgCost iqtyFG iavgRawMasCost iactualCost iworeceiptCost ireceiptCost icosting. 
        DOWN WITH FRAME b. 
    END.
    {mfrtrail.i}
    /* cap nhat xuong GL */
    IF iupdate AND checkUpdate THEN DO:
        FOR EACH CostMonthly WHERE CostMonthly.CostMonthly_year = iyear AND CostMonthly.CostMonthly_month = imonth AND CostMonthly.CostMonthly_Domain = global_domain EXCLUSIVE-LOCK:
            DELETE CostMonthly.
        END.
        /* lay danh sach cac item thang truoc */
        FOR EACH buf_CostMonthly NO-LOCK WHERE buf_CostMonthly.CostMonthly_year = ilastyear AND buf_CostMonthly.CostMonthly_month = ilastmonth AND buf_CostMonthly.CostMonthly_domain = global_domain:
            CREATE tCostMonthly.
            BUFFER-COPY buf_CostMonthly TO tCostMonthly.
            ASSIGN 
            tCostMonthly.CostMonthly_finalized = icheck
            tCostMonthly.CostMonthly_year = iyear
            tCostMonthly.CostMonthly_month = imonth
            .
        END.
        /* danh sach cac item phat sinh trong thang */
        sumqtybyloc = 0.
        FOR EACH buf_tr_hist NO-LOCK 
          WHERE buf_tr_hist.tr_domain = global_domain 
            AND buf_tr_hist.tr_effdate >= DATE(imonth, 1, iyear) 
              AND buf_tr_hist.tr_effdate < DATE(inextmonth, 1, inextyear) 
                AND buf_tr_hist.tr_status <> "" 
                  AND buf_tr_hist.tr_loc <> "TRANSIT"
                    AND tr_ship_type <> "m"
        BREAK BY buf_tr_hist.tr_site BY buf_tr_hist.tr_loc BY buf_tr_hist.tr_serial BY buf_tr_hist.tr_ref BY buf_tr_hist.tr_part:
            sumqtybyloc = sumqtybyloc + buf_tr_hist.tr_qty_loc.
            IF LAST-OF(buf_tr_hist.tr_part) THEN DO:
                CREATE tCostMonthly.
                ASSIGN
                tCostMonthly.CostMonthly_part = buf_tr_hist.tr_part
                tCostMonthly.CostMonthly_um = buf_tr_hist.tr_um
                tCostMonthly.CostMonthly_year = iyear
                tCostMonthly.CostMonthly_month = imonth
                tCostMonthly.CostMonthly_domain = global_domain
                tCostMonthly.CostMonthly_site = buf_tr_hist.tr_site
                tCostMonthly.CostMonthly_loc = buf_tr_hist.tr_loc
                tCostMonthly.CostMonthly_serial = buf_tr_hist.tr_serial
                tCostMonthly.CostMonthly_ref = buf_tr_hist.tr_ref
                tCostMonthly.CostMonthly_finalized = icheck
                tCostMonthly.CostMonthly_qty = 0
                tCostMonthly.CostMonthly_qty_loc = sumqtybyloc
                tCostMonthly.CostMonthly_cost = 0
                tCostMonthly.CostMonthly_amount = 0
                .  
                sumqtybyloc = 0. 
            END.
        END.
        
        sumqtybyloc = 0.
        FOR EACH tCostMonthly BREAK BY tCostMonthly.CostMonthly_site BY tCostMonthly.CostMonthly_part BY tCostMonthly.CostMonthly_loc BY tCostMonthly.CostMonthly_serial 
            BY tCostMonthly.CostMonthly_ref:
            sumqtybyloc = sumqtybyloc + tCostMonthly.CostMonthly_qty_loc.
            sumqtybyitem = sumqtybyitem + tCostMonthly.CostMonthly_qty_loc.
            IF LAST-OF (tCostMonthly.CostMonthly_ref) THEN DO:
                CREATE CostMonthly.
                BUFFER-COPY tCostMonthly TO CostMonthly.
                CostMonthly.CostMonthly_qty_loc = sumqtybyloc.
                sumqtybyloc = 0.
                
            END.
            IF LAST-OF(tCostMonthly.CostMonthly_part) THEN DO:
                flag = FALSE.
                FIND FIRST temptable WHERE temptable.finishGood = tCostMonthly.CostMonthly_part AND temptable.site = tCostMonthly.CostMonthly_site NO-LOCK NO-ERROR.
                IF (AVAILABLE temptable) THEN DO:
                    FOR EACH CostMonthly WHERE CostMonthly.CostMonthly_year = iyear AND CostMonthly.CostMonthly_month = imonth AND CostMonthly.CostMonthly_Domain = global_domain
                        AND CostMonthly.CostMonthly_part = tCostMonthly.CostMonthly_part EXCLUSIVE-LOCK:
                        CostMonthly.CostMonthly_cost = temptable.costing.
                        CostMonthly.CostMonthly_qty = sumqtybyitem.
                        CostMonthly.CostMonthly_amount = CostMonthly.CostMonthly_qty_loc * temptable.costing. 
                        IF (sumqtybyitem > 0  AND CostMonthly.CostMonthly_qty_loc = 0) THEN DO:
                            DELETE CostMonthly.
                        END. 
                        ELSE DO:
                            IF (CostMonthly.CostMonthly_cost = 0 OR CostMonthly.CostMonthly_cost = ?) THEN DO:
                                DELETE CostMonthly.
                            END.
                            ELSE DO:
                                IF (sumqtybyitem = 0) THEN DO:
                                    IF (flag = FALSE) THEN DO:
                                        ASSIGN
                                        CostMonthly.CostMonthly_loc = ""
                                        CostMonthly.CostMonthly_serial = ""
                                        CostMonthly.CostMonthly_ref = ""
                                        flag = TRUE.
                                    END.
                                    ELSE DO:
                                        DELETE CostMonthly.
                                    END.
                                END.
                            END.
                        END.
                    END.
                END. 
                ELSE DO:
                    FIND FIRST temptable WHERE temptable.masterial = tCostMonthly.CostMonthly_part AND temptable.site = tCostMonthly.CostMonthly_site NO-LOCK NO-ERROR.
                    IF (AVAILABLE temptable) THEN DO:
                        FOR EACH CostMonthly WHERE CostMonthly.CostMonthly_year = iyear AND CostMonthly.CostMonthly_month = imonth AND CostMonthly.CostMonthly_Domain = global_domain
                            AND CostMonthly.CostMonthly_part = tCostMonthly.CostMonthly_part EXCLUSIVE-LOCK:
                            CostMonthly.CostMonthly_cost = temptable.avgCost.
                            CostMonthly.CostMonthly_qty = sumqtybyitem.
                            CostMonthly.CostMonthly_amount = CostMonthly.CostMonthly_qty_loc * temptable.avgCost. 
                            IF (sumqtybyitem > 0  AND CostMonthly.CostMonthly_qty_loc = 0) THEN DO:
                                DELETE CostMonthly.
                            END.
                            ELSE DO:
                                IF (CostMonthly.CostMonthly_cost = 0 OR CostMonthly.CostMonthly_cost = ?) THEN DO:
                                    DELETE CostMonthly.
                                END.
                                ELSE DO:
                                    IF (sumqtybyitem = 0) THEN DO:
                                        IF (flag = FALSE) THEN DO:
                                            ASSIGN
                                            CostMonthly.CostMonthly_loc = ""
                                            CostMonthly.CostMonthly_serial = ""
                                            CostMonthly.CostMonthly_ref = ""
                                            flag = TRUE.
                                        END.
                                        ELSE DO:
                                            DELETE CostMonthly.
                                        END.
                                    END.
                                END.
                            END.
                        END.
                    END.
                    ELSE DO: 
                       FIND FIRST mcm WHERE mcm.mcm_month = imonth AND mcm.mcm_year = iyear AND mcm.mcm_part = tCostMonthly.CostMonthly_part AND mcm.mcm_site = tCostMonthly.CostMonthly_site AND mcm.mcm_domain = global_domain NO-ERROR.
                        IF (AVAILABLE mcm) THEN DO:
                            FOR EACH CostMonthly WHERE CostMonthly.CostMonthly_year = iyear AND CostMonthly.CostMonthly_month = imonth AND CostMonthly.CostMonthly_Domain = global_domain
                                AND CostMonthly.CostMonthly_part = tCostMonthly.CostMonthly_part EXCLUSIVE-LOCK:
                                CostMonthly.CostMonthly_cost = mcm_unitcost.
                                CostMonthly.CostMonthly_qty = sumqtybyitem.
                                CostMonthly.CostMonthly_amount = CostMonthly.CostMonthly_qty_loc * CostMonthly.CostMonthly_cost.
                                IF (sumqtybyitem > 0  AND CostMonthly.CostMonthly_qty_loc = 0) THEN DO:
                                    DELETE CostMonthly.
                                END.
                                ELSE DO:
                                    IF (CostMonthly.CostMonthly_cost = 0 OR CostMonthly.CostMonthly_cost = ?) THEN DO:
                                        DELETE CostMonthly.
                                    END.
                                    ELSE DO:
                                        IF (sumqtybyitem = 0) THEN DO:
                                            IF (flag = FALSE) THEN DO:
                                                ASSIGN
                                                CostMonthly.CostMonthly_loc = ""
                                                CostMonthly.CostMonthly_serial = ""
                                                CostMonthly.CostMonthly_ref = ""
                                                flag = TRUE.
                                            END.
                                            ELSE DO:
                                                DELETE CostMonthly.
                                            END.
                                        END.
                                    END.
                                END.
                            END.
                        END.
                        ELSE DO:
                            icosting = 0.
                            FIND FIRST CostMonthly WHERE CostMonthly.CostMonthly_year = ilastyear AND CostMonthly.CostMonthly_month = ilastmonth AND CostMonthly.CostMonthly_Domain = global_domain 
                                AND CostMonthly.CostMonthly_part = tCostMonthly.CostMonthly_part NO-LOCK NO-ERROR.
                            IF (AVAILABLE CostMonthly)THEN DO:
                                icosting = CostMonthly.CostMonthly_cost.
                            END.
                            FOR EACH CostMonthly WHERE CostMonthly.CostMonthly_year = iyear AND CostMonthly.CostMonthly_month = imonth AND CostMonthly.CostMonthly_Domain = global_domain
                                AND CostMonthly.CostMonthly_part = tCostMonthly.CostMonthly_part EXCLUSIVE-LOCK:
                                CostMonthly.CostMonthly_cost = icosting.
                                CostMonthly.CostMonthly_qty = sumqtybyitem.
                                CostMonthly.CostMonthly_amount = CostMonthly.CostMonthly_qty_loc * CostMonthly.CostMonthly_cost.
                                IF (sumqtybyitem > 0  AND CostMonthly.CostMonthly_qty_loc = 0) THEN DO:
                                    DELETE CostMonthly.
                                END.
                                ELSE DO:
                                    IF ((CostMonthly.CostMonthly_cost = 0 AND CostMonthly.CostMonthly_qty_loc = 0) OR CostMonthly.CostMonthly_cost = ?) THEN DO:
                                        DELETE CostMonthly.
                                    END.
                                    ELSE DO:
                                        IF (sumqtybyitem = 0) THEN DO:
                                            IF (flag = FALSE) THEN DO:
                                                ASSIGN
                                                CostMonthly.CostMonthly_loc = ""
                                                CostMonthly.CostMonthly_serial = ""
                                                CostMonthly.CostMonthly_ref = ""
                                                flag = TRUE.
                                            END.
                                            ELSE DO:
                                                DELETE CostMonthly.
                                            END.
                                        END.
                                    END.
                                END.
                            END.
                        END.
                    END.
                END. 
                sumqtybyitem = 0.
            END.
        END.
        
        /* Update GLCode "15420000" transfer "15410000" */
        FOR EACH PostingC NO-LOCK WHERE PostingC.PostingDate >= DATE(imonth, 1, iyear) AND PostingC.PostingDate < DATE(inextmonth, 1, inextyear):
            FOR EACH PostingLineC WHERE PostingLineC.Posting_ID = PostingC.Posting_ID EXCLUSIVE-LOCK:
                FIND FIRST GL WHERE GL.GLCode = "15410000" NO-LOCK NO-ERROR.
                IF (AVAILABLE GL) THEN DO:
                    iupdategl = GL.GL_ID.           
                END.
                FIND FIRST GL WHERE GL.GL_ID = PostingLineC.GL_ID AND GL.GLCode = "15420000" AND iupdategl <> ? NO-LOCK NO-ERROR.
                IF (AVAILABLE GL) THEN DO:
                    PostingLineC.GL_ID = iupdategl.
                END.
            END.
        END.
        
        FOR EACH buf_tr_hist NO-LOCK 
          WHERE buf_tr_hist.tr_domain = global_domain 
          AND buf_tr_hist.tr_effdate >= DATE(imonth, 1, iyear) 
          AND buf_tr_hist.tr_effdate < DATE(inextmonth, 1, inextyear)
          AND (buf_tr_hist.tr_type = "ISS-WO" 
          OR buf_tr_hist.tr_type = "ISS-SO" 
          OR buf_tr_hist.tr_type = "ISS-UNP" 
          OR buf_tr_hist.tr_type = "ISS-PRV" 
          OR buf_tr_hist.tr_type = "CNT-TAG"
          OR buf_tr_hist.tr_type = "ISS-TR" 
          OR buf_tr_hist.tr_type = "RCT-WO" 
          OR buf_tr_hist.tr_type = "RCT-TR" 
          OR buf_tr_hist.tr_type = "RCT-UNP" 
          OR (buf_tr_hist.tr_type = "ISS-COR" AND buf_tr_hist.tr_ship_type <> "M")):
            FOR EACH trgl_det
              WHERE trgl_det.trgl_trnbr = buf_tr_hist.tr_trnbr 
              AND trgl_det.trgl_domain = buf_tr_hist.tr_domain 
              AND trgl_det.trgl_gl_ref <> "" 
            NO-LOCK:
                FOR EACH PostingC USE-INDEX PostingOriginRef  
                  WHERE PostingC.PostingOriginReference = trgl_det.trgl_gl_ref
                NO-LOCK:
                    FOR EACH PostingLineC 
                        WHERE PostingLineC.Posting_ID = PostingC.Posting_ID 
                    EXCLUSIVE-LOCK:
                        IF (buf_tr_hist.tr_type = "ISS-WO")THEN DO:
                            FIND FIRST wo_mstr WHERE wo_mstr.wo_lot = buf_tr_hist.tr_lot AND wo_domain = global_domain NO-LOCK NO-ERROR.
                            IF (AVAILABLE wo_mstr ) THEN DO:
                                FIND FIRST temptable WHERE temptable.finishGood = wo_mstr.wo_part AND temptable.masterial = buf_tr_hist.tr_part AND temptable.site = buf_tr_hist.tr_site NO-LOCK NO-ERROR.
                                IF (AVAILABLE temptable) THEN DO:
                                    IF (postingLineC.PostingLineDebitLC <> 0) THEN DO:
                                        postingLineC.PostingLineDebitLC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc * temptable.avgCost), 0).
                                        postingLineC.PostingLineDebitPC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc * temptable.avgCost), 0).
                                        postingLineC.PostingLineDebitTC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc * temptable.avgCost), 0).
                                        postingLineC.PostingLineDebitCC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc * temptable.avgCost), 0).
                                    END.
                                    IF (postingLineC.PostingLineCreditLC <> 0) THEN DO:
                                        postingLineC.PostingLineCreditLC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc * temptable.avgCost), 0).
                                        postingLineC.PostingLineCreditPC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc * temptable.avgCost), 0).
                                        postingLineC.PostingLineCreditTC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc * temptable.avgCost), 0).
                                        postingLineC.PostingLineCreditCC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc * temptable.avgCost), 0).
                                    END.
                                END.
                            END.
                        END.
                        ELSE IF (buf_tr_hist.tr_type = "RCT-WO") THEN DO:
                            sumrctwo = 0.
                            sumqtyrctwo = 0.
                            FOR EACH tr_hist 
                              WHERE tr_hist.tr_domain = global_domain 
                              AND (tr_hist.tr_type = "ISS-WO" OR tr_hist.tr_type = "RCT-WO")
                              AND tr_hist.tr_lot = buf_tr_hist.tr_lot
                            NO-LOCK :
                               
                                FIND FIRST wo_mstr 
                                  WHERE wo_domain = global_domain 
                                  AND wo_mstr.wo_lot = tr_hist.tr_lot 
                                  AND tr_hist.tr_nbr = wo_mstr.wo_nbr 
                                NO-LOCK NO-ERROR.
                                IF (AVAILABLE wo_mstr) THEN DO:
                                    IF (tr_hist.tr_type = "ISS-WO") THEN DO:
                                        FIND FIRST temptable WHERE temptable.finishGood = wo_mstr.wo_part AND temptable.masterial = tr_hist.tr_part AND temptable.site = tr_hist.tr_site NO-LOCK NO-ERROR.
                                        IF (AVAILABLE temptable) THEN DO:
                                            sumrctwo = sumrctwo + tr_hist.tr_qty_loc * temptable.avgCost.
                                            
                                        END.
                                    END.
                                    ELSE DO:
                                        sumqtyrctwo = sumqtyrctwo + tr_hist.tr_qty_loc.
                                    END.
                                END.
                            END.
                            IF (sumqtyrctwo = 0) THEN DO:
                                sumrctwo = 0.
                                sumqtyrctwo = 0.
                                FOR EACH tr_hist 
                                  WHERE tr_hist.tr_domain = global_domain  
                                  AND (tr_hist.tr_type = "ISS-WO" OR tr_hist.tr_type = "RCT-WO")
                                  AND tr_hist.tr_lot = buf_tr_hist.tr_lot
                                NO-LOCK :
                                    IF (tr_hist.tr_qty_loc > 0) THEN DO:
                                        FIND FIRST wo_mstr 
                                          WHERE wo_domain = global_domain 
                                          AND wo_mstr.wo_lot = tr_hist.tr_lot 
                                          AND tr_hist.tr_nbr = wo_mstr.wo_nbr 
                                        NO-LOCK NO-ERROR.
                                        IF (AVAILABLE wo_mstr) THEN DO:
                                            IF (tr_hist.tr_type = "ISS-WO") THEN DO:
                                                FIND FIRST temptable WHERE temptable.finishGood = wo_mstr.wo_part AND temptable.masterial = tr_hist.tr_part AND temptable.site = tr_hist.tr_site NO-LOCK NO-ERROR.
                                                IF (AVAILABLE temptable) THEN DO:
                                                    sumrctwo = sumrctwo + ROUND((tr_hist.tr_qty_loc * temptable.avgCost), 0).
                                                END.
                                            END.
                                            ELSE DO:
                                                IF (tr_hist.tr_qty_loc > 0) THEN DO:
                                                    sumqtyrctwo = sumqtyrctwo + tr_hist.tr_qty_loc.
                                                END.
                                            END.
                                        END.
                                    END.
                                END.
                                IF (postingLineC.PostingLineDebitLC <> 0) THEN DO:
                                    postingLineC.PostingLineDebitLC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                    postingLineC.PostingLineDebitPC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                    postingLineC.PostingLineDebitTC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                    postingLineC.PostingLineDebitCC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                END.
                                IF (postingLineC.PostingLineCreditLC <> 0) THEN DO:
                                    postingLineC.PostingLineCreditLC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                    postingLineC.PostingLineCreditPC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                    postingLineC.PostingLineCreditTC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                    postingLineC.PostingLineCreditCC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                END.
                                
                            END.
                            ELSE DO:
                                IF (postingLineC.PostingLineDebitLC <> 0) THEN DO:
                                    postingLineC.PostingLineDebitLC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                    postingLineC.PostingLineDebitPC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                    postingLineC.PostingLineDebitTC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                    postingLineC.PostingLineDebitCC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                END.
                                IF (postingLineC.PostingLineCreditLC <> 0) THEN DO:
                                    postingLineC.PostingLineCreditLC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                    postingLineC.PostingLineCreditPC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                    postingLineC.PostingLineCreditTC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                    postingLineC.PostingLineCreditCC = ROUND(ABSOLUTE(sumrctwo * buf_tr_hist.tr_qty_loc / sumqtyrctwo), 0).
                                END.
                            END.
                        END.                       
                        ELSE IF (buf_tr_hist.tr_type = "ISS-SO") THEN DO:
                            FIND FIRST ih_hist WHERE ih_hist.ih_nbr = buf_tr_hist.tr_nbr AND ih_hist.ih_domain = buf_tr_hist.tr_domain AND ih_hist.ih_channel = "RETURN" NO-LOCK NO-ERROR.
                            IF (AVAILABLE ih_hist) THEN DO:
                                FIND FIRST CostMonthly WHERE CostMonthly.CostMonthly_part = buf_tr_hist.tr_part AND CostMonthly.CostMonthly_site = buf_tr_hist.tr_site 
                                      AND CostMonthly.CostMonthly_month = ilastmonth AND CostMonthly.CostMonthly_year = ilastyear NO-LOCK NO-ERROR.
                                IF (AVAILABLE CostMonthly) THEN DO:
                                    IF (postingLineC.PostingLineDebitLC <> 0) THEN DO:
                                        postingLineC.PostingLineDebitLC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                        postingLineC.PostingLineDebitPC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                        postingLineC.PostingLineDebitTC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                        postingLineC.PostingLineDebitCC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                    END.
                                    IF (postingLineC.PostingLineCreditLC <> 0) THEN DO:
                                        postingLineC.PostingLineCreditLC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                        postingLineC.PostingLineCreditPC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                        postingLineC.PostingLineCreditTC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                        postingLineC.PostingLineCreditCC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                    END.
                                END.
                            END.
                            ELSE DO:
                                FIND FIRST CostMonthly WHERE CostMonthly.CostMonthly_part = buf_tr_hist.tr_part AND CostMonthly.CostMonthly_site = buf_tr_hist.tr_site 
                                    AND CostMonthly.CostMonthly_month = imonth AND CostMonthly.CostMonthly_year = iyear AND CostMonthly.CostMonthly_domain = global_domain NO-LOCK NO-ERROR.
                                IF (AVAILABLE CostMonthly) THEN DO:
                                    IF (postingLineC.PostingLineDebitLC <> 0) THEN DO:
                                        postingLineC.PostingLineDebitLC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost), 0).
                                        postingLineC.PostingLineDebitPC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost), 0).
                                        postingLineC.PostingLineDebitTC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost), 0).
                                        postingLineC.PostingLineDebitCC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost), 0).
                                    END.
                                    IF (postingLineC.PostingLineCreditLC <> 0) THEN DO:
                                        postingLineC.PostingLineCreditLC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost), 0).
                                        postingLineC.PostingLineCreditPC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost), 0).
                                        postingLineC.PostingLineCreditTC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost), 0).
                                        postingLineC.PostingLineCreditCC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost), 0).
                                    END.
                                END.
                            END.  
                        END. 
                        
                        ELSE IF (buf_tr_hist.tr_type = "ISS-COR" AND buf_tr_hist.tr_ship_type <> "M") THEN DO:
                            FIND FIRST CostMonthly WHERE CostMonthly.CostMonthly_part = buf_tr_hist.tr_part AND CostMonthly.CostMonthly_site = buf_tr_hist.tr_site 
                                  AND CostMonthly.CostMonthly_month = ilastmonth AND CostMonthly.CostMonthly_year = ilastyear NO-LOCK NO-ERROR.
                            IF (AVAILABLE CostMonthly) THEN DO:
                                IF (postingLineC.PostingLineDebitLC <> 0) THEN DO:
                                    postingLineC.PostingLineDebitLC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                    postingLineC.PostingLineDebitPC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                    postingLineC.PostingLineDebitTC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                    postingLineC.PostingLineDebitCC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                END.
                                IF (postingLineC.PostingLineCreditLC <> 0) THEN DO:
                                    postingLineC.PostingLineCreditLC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                    postingLineC.PostingLineCreditPC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                    postingLineC.PostingLineCreditTC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                    postingLineC.PostingLineCreditCC = ROUND(ABSOLUTE(CostMonthly.CostMonthly_cost * buf_tr_hist.tr_qty_loc), 0).
                                END.
                            END.
                        END.
                        /* ELSE IF (buf_tr_hist.tr_type = "ISS-TR" OR buf_tr_hist.tr_type = "RCT-TR") THEN DO:
                            getidwo = buf_tr_hist.tr_rmks.
                            REPEAT:
                                getindex = INDEX(getidwo," ").
                                IF (getindex <> 0) THEN DO:
                                    getidwo = SUBSTRING(getidwo, getindex + 1).
                                END.
                                ELSE DO:
                                    LEAVE.
                                END.
                            END.
                            FIND FIRST tr_hist 
                              WHERE tr_hist.tr_domain = global_domain 
                              AND tr_hist.tr_effdate >= DATE(imonth, 1, iyear) 
                              AND tr_hist.tr_effdate < DATE(inextmonth, 1, inextyear) 
                              AND tr_hist.tr_lot = getidwo 
                              AND tr_hist.tr_type = "ISS-WO" 
                              AND getidwo <> "" 
                              AND tr_hist.tr_part = buf_tr_hist.tr_part 
                            NO-LOCK NO-ERROR.
                            IF (AVAILABLE tr_hist) THEN DO:
                                FIND FIRST wo_mstr WHERE wo_mstr.wo_lot = tr_hist.tr_lot AND wo_domain = global_domain NO-LOCK NO-ERROR.
                                IF (AVAILABLE wo_mstr ) THEN DO:
                                    FIND FIRST temptable WHERE temptable.finishGood = wo_mstr.wo_part AND temptable.masterial = tr_hist.tr_part AND temptable.site = tr_hist.tr_site NO-LOCK NO-ERROR.
                                    IF (AVAILABLE temptable) THEN DO:
                                        IF (postingLineC.PostingLineDebitLC <> 0) THEN DO:
                                            postingLineC.PostingLineDebitLC = ABSOLUTE(tr_hist.tr_qty_loc * temptable.avgCost).
                                            postingLineC.PostingLineDebitPC = ABSOLUTE(tr_hist.tr_qty_loc * temptable.avgCost).
                                            postingLineC.PostingLineDebitTC = ABSOLUTE(tr_hist.tr_qty_loc * temptable.avgCost).
                                            postingLineC.PostingLineDebitCC = ABSOLUTE(tr_hist.tr_qty_loc * temptable.avgCost).
                                        END.
                                        IF (postingLineC.PostingLineCreditLC <> 0) THEN DO:
                                            postingLineC.PostingLineCreditLC = ABSOLUTE(tr_hist.tr_qty_loc * temptable.avgCost).
                                            postingLineC.PostingLineCreditPC = ABSOLUTE(tr_hist.tr_qty_loc * temptable.avgCost).
                                            postingLineC.PostingLineCreditTC = ABSOLUTE(tr_hist.tr_qty_loc * temptable.avgCost).
                                            postingLineC.PostingLineCreditCC = ABSOLUTE(tr_hist.tr_qty_loc * temptable.avgCost).
                                        END.
                                    END.
                                END.
                            END.
                            ELSE DO:
                                FIND FIRST CostMonthly WHERE CostMonthly.CostMonthly_part = buf_tr_hist.tr_part AND CostMonthly.CostMonthly_site = buf_tr_hist.tr_site 
                                    AND CostMonthly.CostMonthly_month = imonth AND CostMonthly.CostMonthly_year = iyear AND CostMonthly.CostMonthly_domain = global_domain NO-LOCK NO-ERROR.
                                IF (AVAILABLE CostMonthly) THEN DO:
                                    IF (postingLineC.PostingLineDebitLC <> 0) THEN DO:
                                        postingLineC.PostingLineDebitLC = ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost).
                                        postingLineC.PostingLineDebitPC = ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost).
                                        postingLineC.PostingLineDebitTC = ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost).
                                        postingLineC.PostingLineDebitCC = ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost).
                                    END.
                                    IF (postingLineC.PostingLineCreditLC <> 0) THEN DO:
                                        postingLineC.PostingLineCreditLC = ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost).
                                        postingLineC.PostingLineCreditPC = ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost).
                                        postingLineC.PostingLineCreditTC = ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost).
                                        postingLineC.PostingLineCreditCC = ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost).
                                    END.
                                END.
                            END.
                        END.  */
                        ELSE DO:
                            FIND FIRST CostMonthly WHERE CostMonthly.CostMonthly_part = buf_tr_hist.tr_part AND CostMonthly.CostMonthly_site = buf_tr_hist.tr_site 
                                AND CostMonthly.CostMonthly_month = imonth AND CostMonthly.CostMonthly_year = iyear AND CostMonthly.CostMonthly_domain = global_domain NO-LOCK NO-ERROR.
                            IF (AVAILABLE CostMonthly) THEN DO:
                                IF (postingLineC.PostingLineDebitLC <> 0) THEN DO:
                                    postingLineC.PostingLineDebitLC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost),0).
                                    postingLineC.PostingLineDebitPC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost),0).
                                    postingLineC.PostingLineDebitTC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost),0).
                                    postingLineC.PostingLineDebitCC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost),0).
                                END.
                                IF (postingLineC.PostingLineCreditLC <> 0) THEN DO:
                                    postingLineC.PostingLineCreditLC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost),0).
                                    postingLineC.PostingLineCreditPC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost),0).
                                    postingLineC.PostingLineCreditTC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost),0).
                                    postingLineC.PostingLineCreditCC = ROUND(ABSOLUTE(buf_tr_hist.tr_qty_loc  * CostMonthly.CostMonthly_cost),0).
                                END.
                            END.
                        END.
                    END.
                END.
            END.
            
            
        END.
        FOR EACH PostingC WHERE PostingC.PostingDate >= DATE(imonth, 1, iyear) AND PostingC.PostingDate < DATE(inextmonth, 1, inextyear):
            FIND FIRST Journal WHERE Journal.Journal_ID = PostingC.Journal_ID AND Journal.JournalCode = "Auto-P&L" NO-LOCK NO-ERROR.
            IF (AVAILABLE Journal) THEN DO:
                ASSIGN updateposting = 0.
                FOR EACH Posting NO-LOCK WHERE Posting.PostingDate >= DATE(1, 1, iyear) AND Posting.PostingDate <= DATE(12, 31, 2013):
                    FIND FIRST Journal WHERE Journal.Journal_ID = Posting.Journal_ID AND Journal.JournalCode <> "CLO-P&L" AND Journal.JournalCode <> "CLO-BA" NO-LOCK NO-ERROR.
                    IF (AVAILABLE Journal) THEN DO:
                        FIND FIRST Layer WHERE Layer.Layer_ID = Journal.Layer_ID AND Layer.LayerCode = "Official" NO-LOCK NO-ERROR.
                        IF (AVAILABLE Layer) THEN DO:
                            FOR EACH PostingLineC NO-LOCK WHERE PostingLineC.Posting_ID = Posting.Posting_ID :
                                FIND FIRST GL WHERE GL.GL_ID = PostingLineC.GL_ID AND (GL.GLCode BEGINS "5" OR GL.GLCode BEGINS "6" OR GL.GLCode BEGINS "7" OR GL.GLCode BEGINS "8") NO-LOCK NO-ERROR.
                                IF (AVAILABLE GL) THEN DO:
                                    updateposting =  updateposting + PostingLineC.PostingLineDebitLC - PostingLineC.PostingLineCreditLC.           
                                END.
                            END.
                        END.
                    END.
                END.
                MESSAGE updateposting.
                FOR EACH PostingLineC WHERE PostingLineC.Posting_ID = PostingC.Posting_ID EXCLUSIVE-LOCK:
                    IF (postingLineC.PostingLineDebitLC <> 0) THEN DO:
                        postingLineC.PostingLineDebitLC = updateposting.
                        postingLineC.PostingLineDebitPC = updateposting.
                        postingLineC.PostingLineDebitTC = updateposting.
                        postingLineC.PostingLineDebitCC = updateposting.
                    END.
                    IF (postingLineC.PostingLineCreditLC <> 0) THEN DO:
                        postingLineC.PostingLineCreditLC = updateposting.
                        postingLineC.PostingLineCreditPC = updateposting.
                        postingLineC.PostingLineCreditTC = updateposting.
                        postingLineC.PostingLineCreditCC = updateposting.
                    END.
                END.
            END.
        END.
        IF (imonth = 12 AND iyear = 2013) THEN DO:
            FOR EACH CostMonthly WHERE CostMonthly.CostMonthly_part = "100006101" AND CostMonthly.CostMonthly_month = 12 AND CostMonthly.CostMonthly_year = 2013:
                CostMonthly.CostMonthly_cost = CostMonthly.CostMonthly_cost + (36364 / CostMonthly.CostMonthly_qty).
            END.
        END.
    END.
    
    EMPTY TEMP-TABLE temptable.
    EMPTY TEMP-TABLE tCostMonthly.
    EMPTY TEMP-TABLE tempfg.
END.









